package be.android.forap.gui;

import be.android.forap.ForAp;
import be.android.forap.R;
import be.android.forap.dataprovider.ContactIntegrityElement;
import be.android.forap.dataprovider.ContactManager;
import be.android.forap.dataprovider.ContactsIntegrityDb;
import be.android.forap.dataprovider.RawContact;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class ContactInfoActivity extends Activity {

	private static final String TAG = "ContactInfoActivity";

	public static final String INFO_DISPLAYNAME = "INFO_DISPLAYNAME";
	public static final String INFO_PHONE = "INFO_PHONE";
	public static final String INFO_EMAIL = "INFO_EMAIL";
	public static final String INFO_PUBLICKEY = "INFO_PUBLICKEY";
	public static final String INFO_GLOBAL_ID = "INFO_GLOBAL_ID";
	public static final String INFO_DIRTY = "INFO_DIRTY";
	public static final String INFO_DELETED = "INFO_DELETED";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact_info);
		Intent launcher = getIntent();

		String displayName = launcher.getStringExtra(INFO_DISPLAYNAME);
		Log.d(TAG, displayName);
		String phone = launcher.getStringExtra(INFO_PHONE);
		String email = launcher.getStringExtra(INFO_EMAIL);
		String pkey = launcher.getStringExtra(INFO_PUBLICKEY);
		String globalId = launcher.getStringExtra(INFO_GLOBAL_ID);
		boolean dirty = launcher.getBooleanExtra(INFO_DIRTY,false);
		boolean deleted = launcher.getBooleanExtra(INFO_DELETED,false);

		ForAp app = ForAp.getInstance();

		((TextView) findViewById(R.id.cinfo_textview_title)).setText(displayName);
		((TextView) findViewById(R.id.cinfo_textview_phone)).setText(phone);
		((TextView) findViewById(R.id.cinfo_textview_email)).setText(email);
		((TextView) findViewById(R.id.cinfo_textview_public_key)).setText(pkey);
		((TextView) findViewById(R.id.cinfo_textview_accounttype)).setText(app.getForApAccount().type);
		((TextView) findViewById(R.id.cinfo_textview_globalid)).setText(globalId);
		String status = "";
		if(dirty & deleted){
			status = "recently changed (dirty=1) and will be deleted (delete=1)";
		} else {
			if(dirty)
				status = "recently changed (dirty=1)";
			if(deleted)
				status = "will be deleted (deleted=1)";
		}
		ContactsIntegrityDb db = new ContactsIntegrityDb(this);
		db.open();
		ContactIntegrityElement cie = db.getContactIntegrity(globalId);
		db.close();
		if(cie.verifyIntegrity(globalId,pkey)){
			status = "Integrity verified.";
		} else {

			status = "ERROR integrity test failed!!!!!";

		}

		((TextView) findViewById(R.id.cinfo_textview_integrity)).setText(status);
	}
}
